import time
import uvicorn
from fastapi import FastAPI, Request, Response

from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

origins = [
    "http://localhost.tiangolo.com",
    "https://localhost.tiangolo.com",
    "http://localhost",
    "http://localhost:8080",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response


@app.middleware("http")
async def edit(request: Request, call_next):
    name = request.query_params.get("name")
    print(name)
    response: Response = await call_next(request)
    return response


@app.post("/test")
async def test(name: str):
    time.sleep(5)
    return {'code', 123}


if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=8000)